﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DataModel;
using System.Data.Entity;
using DataAccess.Datatypes;

namespace DataAccess
{
    public class SiteManager
    {
        private ModelContainer _data = new ModelContainer();

        //Devuelve todos los sitios
        public IEnumerable<Site> GetSites()
        {
            List<Site> result = new List<Site>();

            foreach (var item in _data.SiteSet)
            {
                Site site = new Site();
                site.id = item.id;
                site.Name = item.Name;
                site.AdminId = item.AdminId;
                site.Style = item.Style;
                site.Description = item.GoodType;
                if (item.Logo == null)
                {
                    site.HasLogo = "No";
                }
                else
                {
                    site.HasLogo = "Yes";
                }

                result.Add(site);
            }

            return result;
        }

        public IEnumerable<Site> GetSitesWithMetadata()
        {
            List<Site> result = new List<Site>();

            foreach (var item in _data.SiteSet.Include(s => s.SiteAttributeSet))
            {
                Site site = new Site();
                site.id = item.id;
                site.Name = item.Name;
                site.AdminId = item.AdminId;
                site.Style = item.Style;
                site.Description = item.GoodType;
                if (item.Logo == null)
                {
                    site.HasLogo = "No";
                }
                else
                {
                    site.HasLogo = "Yes";
                }
                site.Attributes = item.SiteAttributeSet;

                result.Add(site);
            }

            return result;
        }

        //Devuelve n sitios elegidos al azar
        public IEnumerable<Site> GetNSites(int n)
        {
            List<Site> result = new List<Site>();
            IList<SiteSet> sites = _data.SiteSet.ToList();
            Random random = new Random();

            int i = 0;
            while (i < n && sites.Count() > 0)
            {
                int position = random.Next(0, sites.Count());
                SiteSet item = sites.ElementAt(position);
                sites.RemoveAt(position);
                
                Site site = new Site();
                site.id = item.id;
                site.Name = item.Name;
                site.Description = item.GoodType;
                result.Add(site);
            }

            return result;
        }

        //Devuelve el sitio solicitado
        public Site GetSite(int siteId)
        {
            SiteSet item = _data.SiteSet.Find(siteId);
            if (item != null)
            {
                Site site = new Site();
                site.id = item.id;
                site.Name = item.Name;
                site.AdminId = item.AdminId;
                site.Style = item.Style;
                site.Description = item.GoodType;

                if (item.Logo == null)
                {
                    site.HasLogo = "No";
                }
                else
                {
                    site.HasLogo = "Yes";
                }

                return site;
            }

            return null;
        }

        //Devuelve los sitios amigos y los atributos del sitio solicitado
        public Site GetSiteData(int siteId)
        {
            SiteSet item = _data.SiteSet.Find(siteId);
            if (item != null)
            {
                Site site = new Site();
                site.id = item.id;
                site.Name = item.Name;

                site.FriendSites = item.FriendSiteSet;
                site.Attributes = item.SiteAttributeSet;

                return site;
            }

            return null;
        }

        //Devuelve el logo del sitio soliciado
        public byte[] GetSiteLogo(int siteId)
        {
            SiteSet site = _data.SiteSet.Find(siteId);
            return site.Logo;
        }

        //Agrega un sitio
        public void AddSite(string name, byte[] logo, string style, string goodType, string adminId) 
        {
            SiteSet site = new SiteSet();
            site.Name = name;
            site.Logo = logo;
            site.Style = style;
            site.GoodType = goodType;
            site.AdminId = adminId;
            _data.SiteSet.Add(site);
            _data.SaveChanges();
            //Agrego atributos al sitio
            /*foreach (var item in attributes)
            {
                SiteAttributeSet att = new SiteAttributeSet();
                att.Name = item;
                att.SiteId = site.id;
                att.SiteSet = site;
                site.SiteAttributeSet.Add(att);
            }*/
            //crear atributos
            _data.SaveChanges();
        }

        //Modifica un sitio
        public void ModifySite(int id, string name, byte[] logo, string style, string goodType, string adminId)
        {
            if (name == null)
            {
                return;
            }
            SiteSet site = _data.SiteSet.Find(id);
            site.Name = name;
            site.Style = style;
            site.GoodType = goodType;
            site.AdminId = adminId;
            if (logo != null)
            {
                site.Logo = logo;
            }
            _data.SaveChanges();
        }

        //Agrega un atributo a un sitio
        public int AddAttribute(int siteId, string name)
        {
            SiteSet site = _data.SiteSet.Find(siteId);
            
            if (site != null)
            {
                SiteAttributeSet attribute = new SiteAttributeSet();
                attribute.Name = name;
                attribute.SiteId = site.id;
                attribute.SiteSet  = site;
                site.SiteAttributeSet.Add(attribute);
                _data.SaveChanges();
                return attribute.id;
            }

            return -1;
        }

        //Modifica un atributo
        public void ModifyAttribute(int attributeId, string name)
        {
            if (name == null)
            {
                return;
            }
            SiteAttributeSet att = _data.SiteAttributeSet.Find(attributeId);
            if (att != null)
            {
                att.Name = name;
                _data.SaveChanges();
            }
        }

        //Agrega un sitio amigo
        public int AddFriendSite(int siteId, string name, string url, string keyword)
        {
            SiteSet site = _data.SiteSet.Find(siteId);
            
            if (site != null)
            {
                FriendSiteSet fSite = new FriendSiteSet();
                fSite.Url = url;
                fSite.Name = name;
                fSite.Keyword = keyword;
                fSite.SiteId = siteId;
                fSite.SiteSet = site;
                site.FriendSiteSet.Add(fSite);
                _data.SaveChanges();
                return fSite.id;
            }

            return -1;
        }

        //Modifica un sitio amigo
        public void ModifyFriendSite(int fSiteId, string name, string url, string keyword)
        {
            if (name == null)
            {
                return;
            }
            FriendSiteSet fSite = _data.FriendSiteSetSet.Find(fSiteId);
            if (fSite != null)
            {
                fSite.Name = name;
                fSite.Keyword = keyword;
                fSite.Url = url;
                _data.SaveChanges();
            }
        }

        //Elimina un sitio amigo
        public void DeleteFriendSite(int fSiteId)
        {
            FriendSiteSet fSite = _data.FriendSiteSetSet.Find(fSiteId);
            if (fSite != null)
            {
                fSite.SiteSet.FriendSiteSet.Remove(fSite);
                _data.FriendSiteSetSet.Remove(fSite);
                _data.SaveChanges();
            }
        }

        //Devuelve un sitio amigo seleccionado al azar
        public FriendSite GetFriendSite(int siteId)
        {
            FriendSite fSite = new FriendSite();
            SiteSet site = _data.SiteSet.Find(siteId);
            if (site != null)
            {
                List<FriendSiteSet> fSites = site.FriendSiteSet.ToList();
                if (fSites.Count > 0)
                {
                    Random random = new Random();
                    int position = random.Next(0, fSites.Count());
                    fSite.Url = fSites.ElementAt(position).Url;
                    fSite.Name = fSites.ElementAt(position).Name;
                    fSite.Keyword = fSites.ElementAt(position).Keyword;
                }

                return fSite;
            }
            
            fSite.Url = "";
            fSite.Name = "";
            
            return fSite;
        }

        //Devuelve las variables de la plataforma
        public Config GetConfiguration()
        {
            Config config = new Config();
            ConfigurationSet item = _data.ConfigurationSet.First();
            config.InapropiateMax = item.InapropiateMax;
            config.InfractionMax = item.InfractionMax;
            config.RankedGoodsMax = item.RankedGoodsMax;
            return config;
        }

        //Modifica las variables de la plataforma
        public void ModifyConfiguration(int inapMax, int infracMax, int rankMax)
        {
            ConfigurationSet config = _data.ConfigurationSet.First();
            config.InapropiateMax = inapMax;
            config.InfractionMax = infracMax;
            config.RankedGoodsMax = rankMax;
            _data.SaveChanges();
        }

    }
}
